import requests
import pymongo
from urllib.parse import urlparse
import os

client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["mydb2"]
collection = db["bilibili"]

num = 1
while True:
    url = (
        f'https://api.bilibili.com/pgc/season/index/result?st=1&order=3&season_version=-1&spoken_language_type=-1'
        f'&area=-1'
        f'&is_finish=-1&copyright=-1&season_status=-1&season_month=-1&year=-1&style_id=-1&sort=0&page={num}&season_type=1'
        f'&pagesize=20&type=1')

    headers = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
                      "Chrome/135.0.0.0}"
    }

    req = requests.get(url, headers=headers)
    data = req.json()
    print(data)

    for i in data['data']['list']:
        cover_url = i['cover']
        try:
            cover_response = requests.get(cover_url)
            cover_response.raise_for_status()
            parsed_url = urlparse(cover_url)
            file_name = os.path.basename(parsed_url.path)
            with open(f'./covers/{file_name}', 'wb') as f:
                f.write(cover_response.content)
            i['cover_path'] = f'./covers/{file_name}'
            try:
                collection.insert_one(i)
                print(f"成功存储数据: {i['title']}")
            except pymongo.errors.PyMongoError as e:
                print(f"存储数据 {i['title']} 到 MongoDB 失败: {e}")
        except requests.RequestException as e:
            print(f"下载主图 {cover_url} 失败: {e}")
    num += 1




